package com.tju.serviceUcenter.controller;


import com.tju.JwtUtils;
import com.tju.Result;
import com.tju.Vo.UcenterMemberVo;
import com.tju.serviceUcenter.entity.UcenterMember;
import com.tju.serviceUcenter.entity.VO.RegisterVO;

import com.tju.serviceUcenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author gsc
 * @since 2022-01-14
 */
@RestController
@CrossOrigin
@RequestMapping("/educenter/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public Result login(@RequestBody UcenterMember ucenterMember){
        //返回token值
        String token = memberService.login(ucenterMember);
        return Result.ok().data("token",token);
    }

    //注册方法
    @PostMapping("register")
    public Result register(@RequestBody RegisterVO registerVO){
        memberService.register(registerVO);
        return Result.ok();
    }

    //根据token获取用户的信息
    @GetMapping("getMemberInfo")
    public Result getMemberInfo(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库，根据用户id获取用户信息
        System.out.println("getMember方法执行");
        UcenterMember member = memberService.getById(memberId);
        return Result.ok().data("userInfo",member);
    }

    //根据用户id查询用户信息
    @PostMapping("getMemberInfoById/{memberId}")
    public UcenterMemberVo getMemberInfoById(@PathVariable String memberId){
        UcenterMember member = memberService.getById(memberId);
        UcenterMemberVo memberVo = new UcenterMemberVo();
        BeanUtils.copyProperties(member,memberVo);
        return memberVo;
    }

    //根据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberVo getUserInfoOrder(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        UcenterMemberVo ucenterMemberVo = new UcenterMemberVo();
        BeanUtils.copyProperties(member,ucenterMemberVo);
        return ucenterMemberVo;
    }
    //查询某一天的注册人数，添加到统计表中
    @GetMapping("countRegister/{day}")
    public Result countRegister(@PathVariable String day){
        Integer count = memberService.countRegister(day);
        return Result.ok().data("countRegister",count);
    }
}

